package com.lyw.utils;

import org.springframework.lang.Nullable;

import java.awt.*;

public class GetColorUtil {

    private GetColorUtil() {}

    public static int[] getPositionByColor(Point location, Robot robot, String startPosition, String color, int interval) {

        String[] targetColor = color.split("-");
        String[] split = startPosition.split(",");
        String[] startIndex = split[0].split("-");
        String[] endIndex = split[1].split("-");
        int x = (int) location.getX();
        int y = (int) location.getY();

        for (int i = Integer.parseInt(startIndex[0]); i < Integer.parseInt(endIndex[0]); i+=interval) {
            for (int j = Integer.parseInt(startIndex[1]); j < Integer.parseInt(endIndex[1]); j+=interval) {
                Color curColor = robot.getPixelColor(x + i, y + j);
                if (Integer.parseInt(targetColor[0]) == curColor.getRed()
                        && Integer.parseInt(targetColor[1]) == curColor.getGreen()
                        && Integer.parseInt(targetColor[2]) == curColor.getBlue()) {
                    return new int[]{x + i, y + j};
                }
            }
        }

        return null;
    }

    private static int[] computeCharacterPosition(Robot robot, String[] targetColor, int x, int y) {
        System.out.println(System.currentTimeMillis());
        int s = x - 1;
        int e = x + 1;
        int h = y + 1;
        Color leftColor = robot.getPixelColor(s, y);
        Color rightColor = robot.getPixelColor(e, y);
        Color heightColor = robot.getPixelColor(x, h);

        while (Integer.parseInt(targetColor[0]) == leftColor.getRed()
                && Integer.parseInt(targetColor[1]) == leftColor.getGreen()
                && Integer.parseInt(targetColor[2]) == leftColor.getBlue()) {
            leftColor = robot.getPixelColor(--s, y);
        }

        while (Integer.parseInt(targetColor[0]) == rightColor.getRed()
                && Integer.parseInt(targetColor[1]) == rightColor.getGreen()
                && Integer.parseInt(targetColor[2]) == rightColor.getBlue()) {
            rightColor = robot.getPixelColor(++e, y);
        }

        while (Integer.parseInt(targetColor[0]) == heightColor.getRed()
                && Integer.parseInt(targetColor[1]) == heightColor.getGreen()
                && Integer.parseInt(targetColor[2]) == heightColor.getBlue()) {
            heightColor = robot.getPixelColor(x, ++h);
        }

        x = s + ((e - s) / 2);
        return new int[]{x, h};
    }
}
